// MProgressBar.cpp: Implementierung der Klasse MProgressBar.
//
//////////////////////////////////////////////////////////////////////

#include "MProgressBar.h"
//#include "MiriamColors.h"
#include <QPalette>
#include <iostream>
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
/*
///some default colours
const QColor StandardRedFill( 165, 0, 33, 255 );//
const QColor StandardBlueFill( 51, 51, 153, 255 );//

const QColor StandardTextColor(Qt::yellow);//

const QColor MiriamBackgroundColor( 0, 51, 0, 255 );//the darkGreen background
const QColor MiriamBorderColour(0, 255, 0, 255);//the bright green borders
/**/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///constructor
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MProgressBar::MProgressBar(QWidget * parent ):QLabel(parent)
{
    //std::cout<<"start"<<std::endl;
    m_fillColor = QColor( 51, 51, 153, 255 );
    m_dangerColor = QColor( 165, 0, 33, 255 );
//	m_borderColor = StandardTextColor;//MiriamBorderColor;
    m_textColor = QColor(Qt::yellow);
//	m_backgroundColor = MiriamBackgroundColor;
    setFrameShape(QFrame::Box);
	m_orientation = Qt::Horizontal;
	m_showText = true;
    m_value = 50.0;
	m_minimum = 0;
	m_maximum = 100;
	m_minSafe = m_minimum;
	m_maxSafe = m_maximum;
}

MProgressBar::~MProgressBar()
{

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///this fills the widget with the background colour
///calculates how much of the widget should be filled with the fill colour
///fills the 'bar' rectangle
///draws the border
///draws the text
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MProgressBar::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);
    QPainter painter(this);

	//standard for the horizontal orientation
	int pixelsFilled = (m_value - minimum())*double(width() - lineWidth()*2/*0*/)/double(maximum() - minimum());
	//const int penPixelWidth = (painter.pen().width()+1);//a pen width of zero is one pixel wide
	QRect borderRect(/*0*/lineWidth(),lineWidth()/*0*/,width()-lineWidth(), height()-lineWidth());
	QRect filledBarRect(lineWidth(),lineWidth(),pixelsFilled, height() - (lineWidth()*2));
	//in the case of vertical orientation we need to recalulate the filling rectangles
	if(m_orientation ==Qt::Vertical){
        pixelsFilled = (m_value - minimum())*double(height() - lineWidth()/*0*/)/double(maximum() - minimum());
        if(pixelsFilled > height() - lineWidth()) pixelsFilled = height() - lineWidth();
		filledBarRect = QRect(lineWidth() /*0*/, height()-(pixelsFilled + lineWidth()), width()-lineWidth()*2, pixelsFilled /*height()-lineWidth()*2*/ );
	}

	//fill everything with the background colour
	//painter.fillRect(borderRect, QBrush(m_backgroundColor)); //QLabel does this for us
	//draw the 'filled bar' part of the thing
	if(m_danger)	//decide what colours to show
        painter.fillRect(filledBarRect, QBrush(m_dangerColor));
	else
		painter.fillRect(filledBarRect, QBrush(m_fillColor));

	//draw a border - done by QLabel
	//painter.setPen(m_borderColor);
	//painter.drawRect(borderRect);
	//draw the text, if any into the shape - this text is always horizontal
	if(m_showText){
		painter.setPen(m_textColor);
		painter.drawText(QRect(0,0,width(), height()), Qt::AlignCenter, QString::number(m_value));
	}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///this sets the widget value and decides what colour to use for the display
/// if value is 
///it also calls an update on the widget to make sure that the thing is drawn after an update
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MProgressBar::setValue(int value)
{
	if(value > m_maxSafe)
    {
        if(!isDanger())  emit sig_danger();
        setDanger();
    }
	else if(value < m_minSafe)
         {
            if(!isDanger())  emit sig_danger();
		    setDanger();
         }
	     else
         {
             if(isDanger()) emit sig_safe();
             setDanger(false);
         }

	m_value = value;

//	QWidget::update();//this is needed cos otherwise we'll only get very sporadic updates
}

/*!
    \fn MProgressBar::isDanger()
 */
bool MProgressBar::isDanger()
{
    return m_danger;
}


/*!
    \fn MProgressBar::oprientation()
 */
Qt::Orientation MProgressBar::orientation()
{
    return m_orientation;
}
